#ifndef ROOMSMATCHTAIL_H 
#define ROOMSMATCHTAIL_H


#include "json.h"
#include <mutex>
#include <string>
#include <functional>
#include <vector>
#include <map>


#ifdef __cplusplus
extern "C" {
#endif
	

using namespace std;
using callback_t = function<bool(Json::Value table)>;
using callback_send_t = function<bool(char *info)>;


class RoomsMatchTail{
public:
	static RoomsMatchTail *instance();

	bool sendMsgToTail(Json::Value table,  string usbport);

	bool recvMsgFromRooms(char *json);

	bool setRecvCb(callback_t cb);

	bool FirstSearchTail(callback_send_t cb);

	int recvJson(char *data, int length, int residueLength);

	bool openHidraw(string usbport, string devnode);
	
	bool closeHidraw(string usbport);

//#if defined(MODEL_ROOMS)
	void HID_Host_Thd(string usbport);
//#endif
private:
	bool jsonTodata(Json::Value &table, char *json);

	bool sendRequest(char *data, const char *usbport);

	bool HID_SendReq(char *pBuf, int len, int reportId, const char *usbport);

	bool getCmdInfo(char *cmd, vector<string> &info);

	bool HID_ProcessCmd(char *pRecv);

	bool HID_TailRep(char *pRecv);

	RoomsMatchTail() = default;

	~RoomsMatchTail() = default;
private:
	static RoomsMatchTail *sm_match;
	static mutex sm_instanceLock;
	callback_t m_cb = nullptr;

	char m_recvJson[1024] = {0};
	int  m_residueLength = 0;
	int  m_offset = 0;
	map<string, int> m_devicefd;
};

#ifdef __cplusplus
}
#endif
#endif
